package whu.o2o;

import java.util.ArrayList;

import whu.o2o.View.LoadDialog;
import whu.o2o.adapter.DiaryAdapter;
import whu.o2o.adapter.GuideAdapter;
import whu.o2o.adapter.SwitchTextAdapter;
import whu.o2o.util.ConstantData;
import whu.o2o.util.ParseXml;
import whu.o2o.util.Utils;
import whu.o2o.vo.CommentVO;
import whu.o2o.vo.DiaryVO;
import whu.o2o.vo.ImageVO;
import whu.o2o.vo.PersonVO;
import whu.o2o.vo.SelectPersonVO;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import whu.o2o.R;




public class GuideActivity extends BaseActivity implements OnClickListener, OnFocusChangeListener, OnItemClickListener,
		OnScrollListener {
	private TextView mSort;
	private TextView mServiceType;
	private TextView mType;

	private LinearLayout mContainer;
	private View mSortContainerView;
	private View mFeatureContainerView;
	private View mSexContainerView;

	private GuideAdapter mGuideAdapter;
	private ArrayList<PersonVO> mGuideList;
	private ArrayList<PersonVO> mSeniorSearchList;
	private SelectPersonVO mSelectGuide;
	private SelectPersonVO mSeniorSearchGuide;

	private String[] mSortTexts;
	private String[] mServiceTypeTexts;
	private String[] mTypeTexts;

	private String mSeniorUrl;
	private final int SORT_CONTAINER_VIEW_ID = 221;
	private final int FEATURE_CONTAINER_VIEW_ID = 222;
	private final int TYPE_CONTAINER_VIEW_ID = 223;
	private final int SORT_LISTVIEW_ID = 331;
	private final int FEATURE_LISTVIEW_ID = 332;
	private final int TYPE_LISTVIEW_ID = 333;

	/** 高级搜索 http */
	private final int SENIOR_SEARCH_HTTP_MSG_CODE = 1;
	/** 高级搜索 解析xml */
	private final int SENIOR_SEARCH_XML_MSG_CODE = 2;

	private Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case SENIOR_SEARCH_HTTP_MSG_CODE:
				if (msg.obj != null && msg.obj instanceof String) {
					String result = (String) msg.obj;
					if (!TextUtils.isEmpty(result)) {
						parseSeniorSearchData(result);
						return false;
					}
				}

				showErrorToast();
				break;

			case SENIOR_SEARCH_XML_MSG_CODE:
				if (mSeniorSearchList.size() == 0) {
					Toast.makeText(mContext, "暂无数据", Toast.LENGTH_SHORT).show();
				} else {
					
				}

				break;

			}

			LoadDialog.closeDialog();
			return false;
		}
	});

	private ListView mListView;
	private DiaryAdapter mAdapter;
	private ArrayList<DiaryVO> mDiaryList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		initObject();
		initView();
		initListener();
		initData();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.senior:
			//showSeniorSearchDialog();
			break;

		case R.id.search:
			break;

		case R.id.tab_one:
			if (isContainView(mContainer, SORT_CONTAINER_VIEW_ID)) {
				removeContainerViews();
			} else {
				showContainerView(mSortContainerView, R.id.tab_one);
				mSort.setBackgroundResource(R.drawable.table_three1);
				mServiceType.setBackgroundResource(R.drawable.table_three);
				mType.setBackgroundResource(R.drawable.table_three);
			}
			break;

		case R.id.tab_two:
			if (isContainView(mContainer, FEATURE_CONTAINER_VIEW_ID)) {
				removeContainerViews();
			} else {
				showContainerView(mFeatureContainerView, R.id.tab_two);
				mServiceType.setBackgroundResource(R.drawable.table_three1);
				mSort.setBackgroundResource(R.drawable.table_three);
				mType.setBackgroundResource(R.drawable.table_three);
			}
			break;

		case R.id.tab_three:
			if (isContainView(mContainer, TYPE_CONTAINER_VIEW_ID)) {
				removeContainerViews();
			} else {
				showContainerView(mSexContainerView, R.id.tab_three);
				mType.setBackgroundResource(R.drawable.table_three1);
				mSort.setBackgroundResource(R.drawable.table_three);
				mServiceType.setBackgroundResource(R.drawable.table_three);
			}
			break;

		case SORT_CONTAINER_VIEW_ID:
		case FEATURE_CONTAINER_VIEW_ID:
		case TYPE_CONTAINER_VIEW_ID:
			removeContainerViews();
			break;

		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		Intent intent = new Intent(mContext, GuideCardActivity.class);
		intent.putExtra("card", mGuideList.get(position));
		startActivity(intent);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		mListViewLastItem = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (mListViewLastItem == mGuideList.size() - 1) {
			PersonVO guide = mGuideList.get(mGuideList.size() - 1);
			if (guide != null && guide.getPage() != -1) {
				requestData(guide.getPage());
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 && mContainer.getChildCount() > 0) {
			removeContainerViews();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onFocusChange(View view, boolean hasFocus) {
		if (view instanceof EditText) {
			if (hasFocus) {
				Utils.setEditTextCursorLocation((EditText) view);
				Utils.showIme(this, (EditText) view);
				((EditText) view).setHint("");
			} else {
				((EditText) view).setHint("0");
			}
		}

	}

	@Override
	protected void parseData(final String xml) {
		if (mFirstPage) {
			mGuideList.clear();
		}

		mApplication.getThreadPool().execute(new Runnable() {

			@Override
			public void run() {
				ParseXml.getInstance().parsePersonXml(xml, mGuideList);
				mHttpListHandler.sendEmptyMessage(ConstantData.Data.PARSE_XML_MSG_CODE);
			}
		});

	}

	@Override
	protected void setAdapter() {
		if (mAdapter == null) {
			mAdapter = new DiaryAdapter(this, mDiaryList, ConstantData.Data.DEFAULT_DIARY_TYPE);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.notifyDataSetChanged();

			if (mFirstPage) {
				scrollTop(mListView);
			}
		}
	}

	protected void requestSeniorSearchData(int page) {
	}

	protected void parseSeniorSearchData(final String xml) {
		if (mSeniorSearchList == null) {
			mSeniorSearchList = new ArrayList<PersonVO>();
		} else {
			mSeniorSearchList.clear();
		}

		mApplication.getThreadPool().execute(new Runnable() {

			@Override
			public void run() {
				ParseXml.getInstance().parsePersonXml(xml, mSeniorSearchList);
				mHandler.sendEmptyMessage(SENIOR_SEARCH_XML_MSG_CODE);
			}
		});

	}

	private void initObject() {
		mSortTexts = getResources().getStringArray(R.array.container_sort);
		mServiceTypeTexts = getResources().getStringArray(R.array.container_guide_service);
		mTypeTexts = getResources().getStringArray(R.array.container_guide_type);

		mGuideList = new ArrayList<PersonVO>();
		mSelectGuide = new SelectPersonVO();
		
		mDiaryList = new ArrayList<DiaryVO>();

	}

	private void initView() {
		mSort = (TextView) findViewById(R.id.tab_one);
		mServiceType = (TextView) findViewById(R.id.tab_two);
		mType = (TextView) findViewById(R.id.tab_three);
		mContainer = (LinearLayout) findViewById(R.id.container_llyt);
		
		mListView = (ListView) findViewById(R.id.listView);
	}

	private void initListener() {
		backBtnClick();
		findViewById(R.id.senior).setOnClickListener(this);
		findViewById(R.id.search).setOnClickListener(this);
		mSort.setOnClickListener(this);
		mServiceType.setOnClickListener(this);
		mType.setOnClickListener(this);
		//findViewById(R.id.write).setOnClickListener(this);
	}

	private void initData() {
		mSort.setText(mSortTexts[mSelectGuide.getSort()]);

		if (0 == mSelectGuide.getServiceType()) {
			mServiceType.setText("分类查看");
		} else {
			mServiceType.setText(mServiceTypeTexts[mSelectGuide.getServiceType()]);
		}

		mType.setText(mTypeTexts[mSelectGuide.getGuideType()]);

		//requestData(BaseActivity.FIRST_PAGE);
		
		String[] times = { "1分钟前", "2分钟前", "3分钟前" };
		String[] contents = { "武汉大学购物优惠券限时发放！", "元旦购物优惠券疯狂奔袭，快来抢啊！", "出售生活用品若干，有意者联系，QQ：123456789" };
		int[] states = { 0, 0, 0 };

		String[] names = { "姬大哥", "赵大哥", "钟大哥" };
		int[] userIds = { 322, 128, 224 };
		int[] ages = { 32, 18, 24 };
		int[] sexs = { 0, 1, 0 };
		boolean[] validateds = { true, true, true };

		for (int i = 0; i < contents.length; i++) {
			DiaryVO diaryVO = new DiaryVO();

			PersonVO personVO = new PersonVO();
			personVO.setId(userIds[i]);
			personVO.setName(names[i]);
			personVO.setSex(sexs[i]);
			personVO.setAge(ages[i]);
			personVO.setValidated(validateds[i]);

			ArrayList<ImageVO> images = new ArrayList<ImageVO>();
			images.add(new ImageVO());

			ArrayList<CommentVO> comments = new ArrayList<CommentVO>();
			CommentVO commentVO1 = new CommentVO();
			commentVO1.setType(0);
			ArrayList<PersonVO> goodList = new ArrayList<PersonVO>();
			for (int j = 0; j < names.length; j++) {
				PersonVO personVOGood = new PersonVO();
				personVOGood.setName(names[j]);
				goodList.add(personVOGood);
			}
			commentVO1.setGoodList(goodList);

			CommentVO commentVO2 = new CommentVO();
			commentVO2.setType(1);
			PersonVO personVOComment = new PersonVO();
			personVOComment.setName(names[i]);
			commentVO2.setUser(personVOComment);
			commentVO2.setContent(contents[i]);
			comments.add(commentVO1);
			comments.add(commentVO2);

			diaryVO.setUser(personVO);
			diaryVO.setContent(contents[i]);
			diaryVO.setTime(times[i]);
			diaryVO.setSendState(states[i]);
			diaryVO.setImages(images);
			//diaryVO.setComments(comments);

			mDiaryList.add(diaryVO);
		}

		setAdapter();

		// requestData(BaseActivity.FIRST_PAGE);
	}

	private void requestData(int page) {
		
	}

	private void showContainerView(View view, int parentId) {
		if (view == null) {
			view = initContainerView(parentId);
		}

		if (view != null) {
			mContainer.removeAllViews();
			mContainer.addView(view);
		}

	}

	private View initContainerView(int parentId) {
		SwitchTextAdapter adapter = null;
		View view = getLayoutInflater().inflate(R.layout.container_list, null);
		ListView listView = (ListView) view.findViewById(R.id.container_listView);

		switch (parentId) {
		case R.id.tab_one:
			mSortContainerView = view;
			mSortContainerView.setId(SORT_CONTAINER_VIEW_ID);
			listView.setId(SORT_LISTVIEW_ID);
			adapter = new SwitchTextAdapter(this, mSortTexts);
			break;

		case R.id.tab_two:
			mFeatureContainerView = view;
			mFeatureContainerView.setId(FEATURE_CONTAINER_VIEW_ID);
			listView.setId(FEATURE_LISTVIEW_ID);
			adapter = new SwitchTextAdapter(this, mServiceTypeTexts);
			break;

		case R.id.tab_three:
			mSexContainerView = view;
			mSexContainerView.setId(TYPE_CONTAINER_VIEW_ID);
			listView.setId(TYPE_LISTVIEW_ID);
			adapter = new SwitchTextAdapter(this, mTypeTexts);
			adapter.publicSelected = ConstantData.Data.WOMAN_GUIDE;
			break;
		}

		view.setOnClickListener(this);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new MyOnItemClickListener(listView));

		return view;
	}

	private void removeContainerViews() {
		mSort.setBackgroundResource(R.drawable.table_three);
		mServiceType.setBackgroundResource(R.drawable.table_three);
		mType.setBackgroundResource(R.drawable.table_three);
		mContainer.removeAllViews();
	}

	private class MyOnItemClickListener implements OnItemClickListener {
		ListView listView;

		public MyOnItemClickListener(ListView listView) {
			this.listView = listView;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
			switch (listView.getId()) {

			default:
				ListAdapter adapter = listView.getAdapter();

				if (!(view instanceof TextView) || !(adapter instanceof SwitchTextAdapter)) {
					return;
				}

				switch (listView.getId()) {

				case SORT_LISTVIEW_ID:
					mSort.setText(((TextView) view).getText().toString());
					mSelectGuide.setSort(position);
					break;

				case FEATURE_LISTVIEW_ID:
					mSelectGuide.setServiceType(position);
					if (0 == mSelectGuide.getServiceType()) {
						mServiceType.setText("服务类型");
					} else {
						mServiceType.setText(((TextView) view).getText().toString());
					}
					break;

				case TYPE_LISTVIEW_ID:
					mType.setText(((TextView) view).getText().toString());
					mSelectGuide.setGuideType(position);
					break;

				}

				SwitchTextAdapter switchTextAdapter = ((SwitchTextAdapter) adapter);
				switchTextAdapter.publicSelected = position;
				switchTextAdapter.notifyDataSetChanged();

				removeContainerViews();
				requestData(BaseActivity.FIRST_PAGE);

				break;
			}

		}
	}

}
